package com.ncmmall.application.business.goods;

import com.ncmmall.domain.business.goods.subject.GoodsSubject;
import com.ncmmall.domain.business.goods.subject.GoodsSubjectRepository;
import com.ncmmall.domain.business.goods.subject.QSubject;
import com.ncmmall.domain.business.goods.subject.Subject;
import com.ncmmall.domain.business.goods.subject.SubjectRepository;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Splitter;
import com.google.common.primitives.Longs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 专题
 *
 * @author likaikai
 * @date 2019-07-16 15:45
 */
@Service
@Transactional
public class SubjectApplication {

    @Autowired
    private SubjectRepository subjectRepository;

    @Autowired
    private GoodsSubjectRepository goodsSubjectRepository;

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 创建专题
     *
     * @param subject
     */
    public void create(Subject subject) {
        subject.setCreateTime(new Timestamp(System.currentTimeMillis()));
        subject.setStatus(1);

        List<GoodsSubject> goodsSubjects = subject.getGoodsSubjects();
        subject.setGoodsNunber(goodsSubjects.size());
        subjectRepository.save(subject);

        for (GoodsSubject goodsSubject : goodsSubjects) {
            goodsSubject.setSubject(subject);
            goodsSubject.setPlatformType(subject.getPlatformType());
            goodsSubjectRepository.save(goodsSubject);
        }
    }


    /**
     * 编辑专题
     *
     * @param subject
     */
    public void modify(Subject subject, Subject params) {

        List<GoodsSubject> old = subject.getGoodsSubjects();
        goodsSubjectRepository.delete(old);

        subject.setName(params.getName());
        subject.setRank(params.getRank());

        List<GoodsSubject> goodsSubjects = params.getGoodsSubjects();
        goodsSubjects.removeIf(item -> item.getGoods() == null);

        subject.setGoodsNunber(goodsSubjects.size());
        subject.setPlatformType(params.getPlatformType());
        subjectRepository.save(subject);

        for (GoodsSubject goodsSubject : goodsSubjects) {
            goodsSubject.setSubject(subject);
            goodsSubject.setPlatformType(subject.getPlatformType());
            goodsSubjectRepository.save(goodsSubject);
        }
    }

    /**
     * 上架
     *
     * @param subject
     */
    public void enable(Subject subject) {
        subject.setStatus(2);
        subjectRepository.save(subject);
    }

    /**
     * 下架
     *
     * @param subject
     */
    public void disable(Subject subject) {
        subject.setStatus(1);
        subjectRepository.save(subject);
    }

    /**
     * 删除专题
     *
     * @param subject
     */
    public void remove(Subject subject) {
        goodsSubjectRepository.delete(subject.getGoodsSubjects());
        subjectRepository.delete(subject);
    }

    /**
     * 批量禁用
     *
     * @param data
     */
    public void disables(String data) {
        List<Long> ids = Splitter.on(";").omitEmptyStrings().trimResults().splitToList(data).stream().map(Longs::tryParse).collect(Collectors.toList());
        List<Subject> subjects = queryChannelService.findAll(QSubject.subject.id.in(ids), Subject.class);
        for (Subject subject : subjects) {
            disable(subject);
        }
    }

    /**
     * 批量启用
     *
     * @param data
     */
    public void enables(String data) {
        List<Long> ids = Splitter.on(";").omitEmptyStrings().trimResults().splitToList(data).stream().map(Longs::tryParse).collect(Collectors.toList());
        List<Subject> subjects = queryChannelService.findAll(QSubject.subject.id.in(ids), Subject.class);
        for (Subject subject : subjects) {
            enable(subject);
        }
    }
}
